package com.eppear.jpfm.framework.tags;

import java.io.IOException;
import java.util.Map;

import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.SimpleTagSupport;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.eppear.jpfm.framework.context.Context;
import com.eppear.jpfm.framework.util.SysUtil;


/**
 * @ClassName DictTag
 * @Description
 *         数据字典标签,用于值和名称的互转
 * @author JianFeng Zhang(张建锋)
 * @date   2017-02-21 上午12:21:57
 *
 */
public class DictTag extends SimpleTagSupport {

	private static final Logger log = LoggerFactory.getLogger(DictTag.class);
	
	private String dicKey;
	//为空或者不存在时英译汉  否则汉译英
	private String type;
 	
	public void doTag() {
		/*if (keys = null ) return;*/
		String[] keys = dicKey.split("\\.");
		Map data = Context.getDicCache();
		Map inData = (Map)data.get(keys[0]);
		String ret = null;
		if (inData == null) return;
		if(type == null || "".equals(type.trim())){
			ret = inData.get(keys[1]).toString();
		}else{
			ret = SysUtil.getMapkeyByValue(inData, keys[1]).toString();
		}
		 JspWriter out = getJspContext().getOut();
		 try {
			out.write(ret);
			out.flush();
		} catch (IOException e) {
			log.error("处理数据字典标签出错",e);
		}
	}


	public String getDicKey() {
		return dicKey;
	}

	public void setDicKey(String dicKey) {
		this.dicKey = dicKey;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}
}
